package com.million.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.orderVo.CourseOrderVo;
import com.million.orderservice.client.CourseOrderClient;
import com.million.orderservice.client.UcenterMemberClient;
import com.million.orderservice.entity.Order;
import com.million.commonutils.orderVo.UcenterMemberVo;
import com.million.orderservice.entity.PayLog;
import com.million.orderservice.entity.vo.OrderInfoVo;
import com.million.orderservice.entity.vo.QueryOrder;
import com.million.orderservice.mapper.OrderMapper;
import com.million.orderservice.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.orderservice.service.PayLogService;
import com.million.orderservice.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    @Autowired
    private CourseOrderClient courseOrderClient;

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayLogService payLogService;

    //根据课程id生成订单接口 返回订单号
    @Override
    public String addOrderInfo (String courseId, String jwtTokenId) {

        if(StringUtils.isEmpty(jwtTokenId)){
            return null;
        }
        //根据用户id 调用用户信息 jwtTokenId就是用户id
        UcenterMemberVo memberVo = ucenterMemberClient.getMemberInfoById(jwtTokenId);

        //根据课程id 调用课程信息
        CourseOrderVo orderCourseInfo = courseOrderClient.getOrderCourseInfo(courseId);
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //订单号
        order.setCourseId(orderCourseInfo.getId());
        order.setCourseCover(orderCourseInfo.getCover());
        order.setCourseTitle(orderCourseInfo.getTitle());
        order.setTeacherName(orderCourseInfo.getTeacherName());
        order.setMemberId(jwtTokenId);
        order.setNickname(memberVo.getNickname());
        order.setMobile(memberVo.getMobile());
        order.setTotalFee(orderCourseInfo.getPrice());
        order.setPayType(1); //微信支付
        order.setStatus(0); //未支付
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }

    //根据订单号 得到订单的信息
    @Override
    public Order getOrderInfo (String orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        return baseMapper.selectOne(queryWrapper);
    }

    //根据课程id 和用户id 查询订单状态
    @Override
    public boolean selectOrderStatus (String courseId, String memberId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("status",1);
        int count = orderService.count(queryWrapper);
        return count >0;
    }

    //分页条件查询订单信息
    @Override
    public Page<OrderInfoVo> queryOrderPage (Page<OrderInfoVo> orderInfoVoPage, QueryOrder queryOrder) {
        String orderNo = queryOrder.getOrderNo();
        if(!StringUtils.isEmpty(orderNo)){
            return baseMapper.getOrderInfoVo(orderInfoVoPage,orderNo);
        }
        return baseMapper.getOrderInfoVoList(orderInfoVoPage);
    }

    //根据订单号删除订单信息 包括删除订单日志
    @Override
    public void deleteOrderInfo (String orderNo) {
        //根据订单号删除订单表信息 逻辑删除
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        baseMapper.delete(queryWrapper);
        //根据订单号删除支付信息  逻辑删除
        QueryWrapper<PayLog> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        payLogService.remove(wrapper);
    }
}
